import javafx.data.pull.Event;
import com.jfxia.arkles.EventNode;
import com.jfxia.arkles.Handler;


// These classes will help store the data we extract from the XML
class Customer {
	var name:String;
	var address:String;
}
class Product {
	var name:String;
	var price:String;
}

// There are the actual variables we'll populate from the XML
var title:String;
var customers:Customer[];
var products:Product[];

// This is the event tree, mirroring the XML file structure
def shopEventTree = EventNode {
	name: "shop"
	onStart: function(ev:Event) { println("Start <shop>"); }
	content: [
		EventNode {
			name: "title"
			onText: function(ev:Event) { title=ev.text; }
		} ,
		EventNode {
			name: "customers"
			content: [
				EventNode {
					name: "customer"
					var c:Customer;
					onStart: function(ev:Event) { c=Customer{}; }
					content: [
						EventNode {
							name: "name"
							onText: function(ev:Event) {  c.name=ev.text; }
						} ,
						EventNode {
							name: "address"
							onText: function(ev:Event) {  c.address=ev.text; }
						}
					]
					onEnd: function(ev:Event) { insert c into customers; }
				}
			]
		} ,
		EventNode {
			name: "products"
			content: [
				EventNode {
					name: "product"
					var p:Product;
					onStart: function(ev:Event) { p=Product{}; }
					content: [
						EventNode {
							name: "name"
							onText: function(ev:Event) {  p.name=ev.text; }
						} ,
						EventNode {
							name: "price"
							onText: function(ev:Event) {  p.price=ev.text; }
						}
					]
					onEnd: function(ev:Event) { insert p into products; }
				}
			]
		}
	]
	onEnd: function(ev:Event) { println("End <shop>"); }
}

def xh:Handler = Handler {
	rootEventNode: shopEventTree;
	input: (new java.net.URL("{__DIR__}test.xml")).openStream();
}
xh.parse();
// Print results
println("{title}");
for(c in customers) println("Customer: {c.name}, {c.address}");
for(p in products) println("Product: {p.name}, {p.price}");


